﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using Emgu.CV.Structure;
using System.Threading;

namespace SoundPlugin
{
    /// <summary>
    /// Interaction logic for Shapes.xaml
    /// </summary>
    public partial class Shapes : UserControl
    {
        #region Properties
        private Dictionary<Emgu.CV.Contour<System.Drawing.Point>, System.Windows.Controls.Image> shapes;
        private EventHandler mouseHandler;
        private Bitmap blankImage;
        private System.Windows.Controls.Image markerImage;
        private SoundToShapeMenu soundToShapeMenu;
        private ReaderWriterLockSlim soundToShapeMenuLock = new ReaderWriterLockSlim();

        public SoundToShapeMenu SoundToShapeMenu
        {
            get 
            {
                try
                {
                    this.soundToShapeMenuLock.EnterReadLock();
                    return soundToShapeMenu;
                }
                finally 
                {
                    this.soundToShapeMenuLock.ExitReadLock();
                }
            }
            set 
            {
                try
                {
                    this.soundToShapeMenuLock.EnterWriteLock();
                    soundToShapeMenu = value;
                }
                finally 
                {
                    this.soundToShapeMenuLock.ExitWriteLock();
                }
            }
        }

        public Bitmap BlankImage
        {
            get { return blankImage; }
            set { blankImage = value; }
        }

        public EventHandler MouseHandler
        {
            get { return mouseHandler; }
            set { mouseHandler = value; }
        }
        #endregion

        #region Constructors
        public Shapes(EventHandler mouseHandler)
        {
            InitializeComponent();
            this.mouseHandler = mouseHandler;
            this.shapes = new Dictionary<Emgu.CV.Contour<System.Drawing.Point>, System.Windows.Controls.Image>();
        }
        #endregion

        #region Internal Methods
        internal void ChangeImage(Bitmap image) 
        {   
            this.MainGrid.Children.Clear();
            this.shapes.Clear();
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            MyImageConverter converter = new MyImageConverter();
            img.Source = (BitmapSource)converter.Convert(image, null, null, null);
            img.MouseLeftButtonUp += new MouseButtonEventHandler(mouseHandler);
            img.Opacity = 0.4;
            this.MainGrid.Children.Add(img);
        }

        internal void AddContour(Emgu.CV.Contour<System.Drawing.Point> contour, System.Drawing.Color color)
        {
            if (this.blankImage == null) return;
            System.Windows.Controls.Image image;
            if (!this.shapes.TryGetValue(contour , out image))
            {
                image = new System.Windows.Controls.Image();
                Emgu.CV.Image<Bgr , byte> emguImage = new Emgu.CV.Image<Bgr , byte>(this.blankImage);
                emguImage = emguImage.CopyBlank();
                emguImage.FillConvexPoly(contour.ToArray(), new Bgr(color));
                emguImage.Draw(contour, new Bgr(color), 3);
                Bitmap bitmap = new Bitmap(emguImage.Bitmap);
                bitmap.MakeTransparent(System.Drawing.Color.Black);
                MyImageConverter converter = new MyImageConverter();
                image.Source = (BitmapSource)converter.Convert(bitmap, null, null, null);
                image.Opacity = 0.6;
                this.shapes.Add(contour, image);
                this.MainGrid.Children.Insert(0, image);
            } 
        }

        internal void RemoveContour(Emgu.CV.Contour<System.Drawing.Point> contour)
        {
            System.Windows.Controls.Image image;
            if (this.shapes.TryGetValue(contour, out image))
            {
                this.MainGrid.Children.Remove(image);
                this.shapes.Remove(contour);
            }
        }

        internal void ShowSoundToShapeMenu() 
        {
            if (this.SoundToShapeMenu != null && !this.MainGrid.Children.Contains(this.soundToShapeMenu)) 
            {
                this.MainGrid.Children.Insert(this.MainGrid.Children.Count, this.SoundToShapeMenu);
            }
        }

        internal void HideSoundToShapeMenu() 
        {
            if (this.soundToShapeMenu != null && this.MainGrid.Children.Contains(this.soundToShapeMenu))
            {
                this.MainGrid.Children.Remove(this.soundToShapeMenu);
            }
        }

        internal void AddMarkerImage(Bitmap image) 
        {
            if (this.markerImage == null) 
            {
                this.markerImage = new System.Windows.Controls.Image();
            }
            if (this.MainGrid.Children.Contains(markerImage)) 
            {
                this.MainGrid.Children.Remove(markerImage);
            }

            MyImageConverter converter = new MyImageConverter();
            this.markerImage.Source = (BitmapSource)converter.Convert(image, null, null, null);
            //this.MainGrid.Children.Add(this.markerImage);
        }
        #endregion
    }
}
